//
//  BCMyViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCMyViewController.h"
#import "BCMineHeaderCollectionViewCell.h"
#import "BCMineDataCollectionViewCell.h"
#import "BCMineControlCollectionViewCell.h"

@interface BCMyViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *mineCollectionView;
@end

@implementation BCMyViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_initNavgationBar];
    [self p_initCollectionView];
}

#pragma mark - View init
- (void)p_initNavgationBar {
    self.title = @"我的";
    self.navigationController.navigationBar.barTintColor = BCColor(0.19, 0.63, 0.34, 1);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mineCollectionView.backgroundColor = [UIColor whiteColor];
    self.mineCollectionView.delegate = self;
    self.mineCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mineCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.mineCollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.view addSubview:self.mineCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        return 3;
    }
    else {
        return 3;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        static NSString *ID = @"MineTitleCell";
        BCMineHeaderCollectionViewCell *cell = [BCMineHeaderCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else if (indexPath.section == 1) {
        
        static NSString *ID = @"MineDataCell";
        BCMineDataCollectionViewCell *cell = [BCMineDataCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.titleLable.text = @"账户余额";
            cell.valueLable.text = @"1000.00元";
        }
        else if (indexPath.row == 1) {
            cell.titleLable.text = @"我的积分";
            cell.valueLable.text = @"800";
        }
        else {
            cell.titleLable.text = @"优惠券";
            cell.valueLable.text = @"3";
        }
        
        return cell;
    }
    else {
        
        static NSString *ID = @"MineControlCell";
        BCMineControlCollectionViewCell *cell = [BCMineControlCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.titleLable.text = @"我的收藏";
            cell.iconImageView.image = [UIImage imageNamed:@"star"];
        }
        else if (indexPath.row == 1) {
            cell.titleLable.text = @"联系客服";
            cell.iconImageView.image = [UIImage imageNamed:@"phone"];
        }
        else {
            cell.titleLable.text = @"关于我们";
            cell.iconImageView.image = [UIImage imageNamed:@"home"];
        }

        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    if (section == 0) {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
    else if(section == 1) {
        UIEdgeInsets top = {5,1,0,1};
        return top;
    }
    else {
        UIEdgeInsets top = {5,1,0,1};
        return top;
    }
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(self.view.frame.size.width, 143.f);
    }
    else if (indexPath.section == 1) {
        return CGSizeMake((self.view.frame.size.width-12.f)/3, 71.f);
    }
    else {
        return CGSizeMake(self.view.frame.size.width-2.f, 50.f);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else if (section == 1) {
        return 5.f;
    }
    else {
        return 0;
    }
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 2) {
        return 5.f;
    }
    else {
        return 0;
    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell %@,%@ selected",@(indexPath.section),@(indexPath.row));
}

@end
